package com.zmn.oms.business.impl.user;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.consts.GlobalConsts;
import com.zmn.oms.business.interfaces.user.OrderUserBService;
import com.zmn.oms.common.utils.MobileUtil;
import com.zmn.oms.model.entity.detail.OrderDetail;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.services.interfaces.detail.OrderDetailService;
import com.zmn.oms.services.interfaces.work.OrderWorkService;
import com.zmn.uuc.common.dio.user.UserBindMobileNoCaptchaByUserIdDIO;
import com.zmn.uuc.common.dro.user.UserDetailDRO;
import com.zmn.uuc.dubbo.interfaces.user.UserBindRemoteService;
import com.zmn.uuc.dubbo.interfaces.user.UserListRemoteService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 工单用户
 *
 * @author: LuJia
 * @version: v1.0
 * @since: 2019/11/24 16:34
 **/
@Service
@Slf4j
public class OrderUserBServiceImpl implements OrderUserBService {

    @Autowired
    private OrderWorkService orderWorkService;
    @Autowired
    private OrderDetailService orderDetailService;

    @Reference(version = com.zmn.uuc.common.constant.DubboConsts.INTERFACE_VERSION, check = false)
    private UserBindRemoteService userBindRemoteService;
    @Reference(version = com.zmn.uuc.common.constant.DubboConsts.INTERFACE_VERSION, check = false)
    private UserListRemoteService userListRemoteService;


    @Override
    public void regularizedUserByOrderId(Long orderId, Long workId) {
        OrderWork orderWork = orderWorkService.findOrderWorkByKey(orderId, workId);
        if (NumberUtil.isNullOrZero(orderWork.getUserId())) {
            return;
        }

        // uuc 查询个人用户信息
        ResponseDTO<UserDetailDRO> userResponseDTO = userListRemoteService.getUserDetailById(orderWork.getUserId(), false);
        UserDetailDRO userDetailDRO = userResponseDTO.getData();
        if (userDetailDRO == null) {
            return;
        }

        if (Objects.equals(userDetailDRO.getTempFlag(), GlobalConsts.NO)) {
            // 正式用户，不处理
            return;
        }

        OrderDetail orderDetail = orderDetailService.findOrderDetailByKey(orderId);

        // 非手机号，不处理
        if (!StringUtil.isMobile(orderDetail.getTelephone())) {
            return;
        }

        // 调用绑定接口将临时用户转为正式用户
        UserBindMobileNoCaptchaByUserIdDIO userBindMobileNoCaptchaByUserIdDIO = new UserBindMobileNoCaptchaByUserIdDIO();
        userBindMobileNoCaptchaByUserIdDIO.setMobile(orderDetail.getTelephone());
        userBindMobileNoCaptchaByUserIdDIO.setUserId(orderWork.getUserId());
        ResponseDTO<Boolean> responseDTO = userBindRemoteService.bindMobileNoCaptchaByUserId(userBindMobileNoCaptchaByUserIdDIO);
        log.info("调用绑定接口将临时用户转为正式用户： userId {}, ResponseDTO [{}]", orderWork.getUserId(), responseDTO);
    }
}